package dao

import (
	"errors"
	__ "shop-microservices/basic/goods_proto"
	"shop-microservices/handler/models"
)

// WelcomeSearchShopList 首页搜索商品列表
func WelcomeSearchShopList(in *__.WelcomeSearchShopListReq) ([]*__.WelcomeSearchShopList, error) {
	var brand models.Brand
	list, err := brand.WelcomeSearchShopList(in.BrandName, int(in.Page), int(in.PageSize))
	if err != nil {
		return nil, errors.New("查询失败")
	}
	var List []*__.WelcomeSearchShopList
	for _, allGoods := range list {
		List = append(List, &__.WelcomeSearchShopList{
			Id:               int64(allGoods.Id),
			BrandLogo:        allGoods.BrandLogo,
			GoodsName:        allGoods.GoodsName,
			BrandDescription: allGoods.BrandDescription,
			GoodsPrice:       float32(allGoods.GoodsPrice),
			BrandName:        allGoods.BrandName,
			GoodsUrl:         allGoods.GoodsUrl,
			GoodsDesc:        allGoods.GoodsDesc,
			GoodsStock:       int64(allGoods.GoodsStock),
			GoodsStatus:      int64(allGoods.GoodsStatus),
			Sales:            int64(allGoods.Sales),
		})
	}
	return List, nil
}
func WelcomeSearchShow(in *__.WelcomeSearchReq) (models.Brand, error) {
	var err error
	var b models.Brand
	var h models.Hot
	b.BrandName = in.BrandName
	if err = b.GetBrandName(); err != nil {
		return models.Brand{}, errors.New("查询失败")
	}
	if b.Id == 0 {
		return models.Brand{}, errors.New("商品不存在")
	}
	if err = b.BrandDetail(); err != nil {
		return models.Brand{}, errors.New("查询失败")
	}
	h.BrandId = b.Id
	if err = h.GetHotBrandId(); err != nil {
		return models.Brand{}, errors.New("查询品牌失败")
	}
	if h.Id == 0 {
		h.BrandName = b.BrandName
		h.Hot = 1
		if err = h.AddBrandHot(); err != nil {
			return models.Brand{}, errors.New("添加失败")
		}
	} else {
		h.Hot++
		if err = h.Updated(); err != nil {
			return models.Brand{}, errors.New("修改失败")
		}
	}
	return b, nil
}
